﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abreu.Base.ViewModels.Interfaces;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Controls;
//using Abreu.Base.ViewModels.Orchestrators;
using CorpAbreu.Common.BaseClasses;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;
using CorpAbreu.Common.Models;
using Abreu.Base.ViewModels.Managers;

namespace Abreu.Base.ViewModels
{
    public class MainVM : IViewModel
    {

        List<IManager> _Orchestrators;
        public List<IManager> Orchestrators
        {
            get
            {
                return _Orchestrators;
            }
            set
            {
                this._Orchestrators = value;
            }
        }

        public IManager CurrentOrchestrator { get; set; }

        

        public NavigationPaneVM NavigationPaneVM
        {
            get
            {
                if (_NavigationPaneVM == null)
                {
                    MenuDataSource mnDS = new MenuDataSource();
                    List<CorpAbreu.Common.Models.Menu> rootMenu = mnDS.GetMenuTree(6);
                    _NavigationPaneVM = new NavigationPaneVM(this, rootMenu, this.ContainerVM);
                }
                return _NavigationPaneVM;
            }
            set
            {
                _NavigationPaneVM = value;
            }
        }
        public NavigationPaneVM _NavigationPaneVM;

        public TabManagerVM ContainerVM
        {
            get
            {
                if (_ContainerVM == null)
                {
                    _ContainerVM = new TabManagerVM(this);
                }

                return _ContainerVM;
            }
            set
            {
                _ContainerVM = value;
            }
        }
        private TabManagerVM _ContainerVM;

        public TabItem SelectedTabItem
        {
            get { return _SelectedTabItem; }
            set
            {
                _SelectedTabItem = value;
                OnPropertyChanged("SelectedTabItem");
            }
        }
        private TabItem _SelectedTabItem;


        public int ApplicationIndex
        {
            get { return _ApplicationIndex; }
            set
            {
                _ApplicationIndex = value;
                OnPropertyChanged("ApplicationIndex");
            }
        }
        private int _ApplicationIndex;

        public MainVM()
        {

            this.Orchestrators = new List<IManager>();

        }

        public IViewModel CreateInstance(string ViewModelName)
        {
            Type vmType = Type.GetType(ViewModelName);

            IManager vm = Activator.CreateInstance(vmType) as IManager;

            if (!this.Orchestrators.Exists(m => m.GetType().FullName.Equals(ViewModelName.Substring(0, ViewModelName.IndexOf(',')))))
            {
                this.Orchestrators.Add(vm);
                vm.Parent = this;
            }
            else
            {
                IViewModel ivm = this.Orchestrators.Single(m => m.GetType().FullName.Equals(ViewModelName.Substring(0, ViewModelName.IndexOf(','))));
                return ivm;
            }
            return vm;
        }



        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }
        }

        public string Name
        {
            get { return "Main"; }
        }



        internal void ShowThis(string name, UserControl userControl, IViewModel ViewModel)
        {
            this.ContainerVM.AddTab(name, userControl, ViewModel);
        }


        public IViewModel Parent
        {
            get;
            set;
        }

        internal void RemoveTab(TabContainerItem tabContainerItem)
        {
            this.ContainerVM.RemoveTab(tabContainerItem);

        }
    }
}
